package b1.mobile.blackberry.entity;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;

/**
 * Application configuration store in Blackberry Persistent Store.
 * Configuration includes 
 * [ServerAddress]
 * [IsSSL]
 * [CompanyDB]
 * [UserCode]
 * [PhoneNum]
 * 
 * @author I067614
 * 
 */
public class AppConfiguration implements Persistable {	
	public String getServerAddress() {
		return serverAddress;
	}

	public void setServerAddress(String serverAddress) {
		this.serverAddress = serverAddress;
	}

	public boolean isSSL() {
		return isSSL;
	}

	public void setSSL(boolean isSSL) {
		this.isSSL = isSSL;
	}

	public String getCompanyDB() {
		return companyDB;
	}

	public void setCompanyDB(String companyDB) {
		this.companyDB = companyDB;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	private String serverAddress = "";
	private boolean isSSL = false;
	private String companyDB = "";
	private String userCode = "";
	private String phoneNum = "";

	private static final long ID = 0x5d8a91784555e0f8L;

	/**
	 * Save Configuration into Persistent Store.
	 * @param myData
	 */
	public static void saveConfiguration(AppConfiguration myData) {

		if (myData == null) {
			return;
		}

		PersistentObject dataStore = PersistentStore.getPersistentObject(ID);

		synchronized (dataStore) {
//			if (dataStore.getContents() == null) {
				dataStore.setContents(myData);
				dataStore.commit();
//			}
		}
	}

	/**
	 * Load Configuration from Persistent Store.
	 * @return
	 */
	public static AppConfiguration getConfiguration() {
		PersistentObject dataStore = PersistentStore.getPersistentObject(ID);

		AppConfiguration myData = new AppConfiguration();
		synchronized (dataStore) {
			if (dataStore.getContents() != null) {
				myData = (AppConfiguration) dataStore.getContents();
			}
		}
		
		return myData;
	}
}
